<template>
    <div class="subtmpl">
        <div class="left" @click="substrict">-</div>
        <div class="middle">{{resObj.count}}</div>
        <div class="right" @click="add">+</div>
    </div>
</template>
<style scoped>
    .subtmpl div{
        display: inline-block;
        padding:5px;
        border:1px solid rgba(92,92,92,0.3);
    }
    .subtmpl .left,.subtmpl .right,.middle{
        width: 30px;
        height: 25px;
        text-align: center;
        line-height: 15px;
        border-radius: 3px;
    }
    .middle{
        width: 40px;
        margin-bottom: 10px;
    }
</style>
<script>
    const  ADD = 'add';
    const  SUBSTRICT = 'substrict';
    export default{
        data(){
            return{
                resObj:{
                    type:ADD,
                    goodsid:0,
                    count:0
                }
            }
        },
        props:['initcount','goodsid']   //initcount:购物车中选择商品数量  goodsid：购物车商品id
        ,
        created(){
            this.resObj.count = this.initcount>1?this.initcount:1;
            this.resObj.goodsid = this.goodsid>0?this.goodsid:0;
        },
        methods:{
//            1.0 加1
            add(){
                this.resObj.count++;
                this.resObj.type = ADD;
                this.notiflycount();//在执行++后执行notiflycount方法向父组件传递count的值
            },
//            2.0 减1
            substrict(){
                if(this.resObj.count <= 1){
                    this.resObj.count = 1;
                }
                this.resObj.count--;
                this.resObj.type = SUBSTRICT;
                this.notiflycount();//在执行--后执行notiflycount方法向父组件传递count的值
            },
//            3.0 将最新的对象传给父组件
            notiflycount(){
                let key = 'count';
                this.$emit(key,this.resObj);  //当执行了这个代码后会自动触发goodsinfo.vue中的getcount方法
            }
        }
    }
</script>